import router from 'umi/router';
import { message } from 'antd';
import {
  getList,
  deleteBatch,
  add,
  edit,
  getAllList,
  getToken,
  toggleMainD,
  getAllDevices,
  synDevice,
} from './services';

export default {
  namespace: 'equipment',
  state: {
    loading: false,
    list: [],
    allList: [], //所有设备组
    allDevList: [], //所有设备
    total: 10,
    pageSize: 10,
    pageNum: 1,
    btnLoading: false,
  },

  effects: {
    *getList({ payload }, { call, put }) {
      try {
        yield put({
          type: 'changeLoading',
          payload: true,
        });
        const res = yield call(getList, payload);
        //debugger
        yield put({
          type: 'save',
          payload: {
            list: res.data,
          },
        });
      } catch (e) {
        message.error(e.message);
      } finally {
        yield put({
          type: 'changeLoading',
          payload: false,
        });
      }
    },

    *getToken({ payload, callback }, { call, put }) {
      try {
        yield put({
          type: 'changeBtnLoading',
          payload: true,
        });
        const res = yield call(getToken, payload);
        callback(res.data);
      } catch (e) {
        message.error(e.message);
      } finally {
        yield put({
          type: 'changeBtnLoading',
          payload: false,
        });
      }
    },

    *toggleMainD({ payload, callback }, { call, put }) {
      try {
        yield call(toggleMainD, payload);
        callback();
      } catch (e) {
        message.error(e.message);
      }
    },

    //获取所有设备组
    *getAllList({ payload, callback }, { call, put }) {
      try {
        const res = yield call(getAllList, payload);
        if (res.data.length > 0) {
          callback(res.data[0]);
        }
        yield put({
          type: 'save',
          payload: {
            allList: res.data,
          },
        });
      } catch (e) {
        message.error(e.message);
      }
    },
    //获取所有设备
    *getAllDevices({ payload, callback }, { call, put }) {
      try {
        const res = yield call(getAllDevices, payload);
        yield put({
          type: 'save',
          payload: {
            allDevList: res.data,
          },
        });
        callback();
      } catch (e) {
        message.error(e.message);
      }
    },

    *add({ payload, callback }, { call, put, select }) {
      try {
        yield call(add, payload);
        callback();
        message.info('添加成功');
      } catch (e) {
        message.error(e.message);
      }
    },

    *delete({ payload, callback }, { call, put, select }) {
      try {
        yield call(deleteBatch, payload);
        callback();
        message.info('删除成功');
      } catch (e) {
        message.error(e.message);
      }
    },

    *edit({ payload, callback }, { call, put, select }) {
      try {
        yield call(edit, payload);
        message.info('修改成功');
        callback();
      } catch (e) {
        message.error(e.message);
      }
    },

    //同步设备
    *synDevice({ payload, callback }, { call, put, select }) {
      try {
        debugger;
        yield call(synDevice, payload);
        message.info('同步成功');
        callback();
      } catch (e) {
        message.error(e.message);
      }
    },

  },

  reducers: {
    changeLoading(state, { payload }) {
      return {
        ...state,
        loading: payload,
      };
    },

    changeBtnLoading(state, { payload }) {
      return {
        ...state,
        btnLoading: payload,
      };
    },

    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
